#include <stdio.h>
#include <stdlib.h>

int main (int argc, char const* argv[])
{
    int c, s, n, t, ans;

    scanf("%d", &c);
    while (c--) {
        scanf("%d %d", &s, &n);

        ans = s; t = 0;
        while (t < n) {
            if ((ans % 4 == 0 && ans % 100 != 0) || ans % 400 == 0) {
                t++;
            }
            ans++;
        }

        printf("%d\n", ans-1);
    }

    return 0;
}
